<?php
/**
 * @file trynotice.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015-4-21 下午7:50:49 $
 * @brief 试用中心公告业务层 $
 */
namespace haibao\cms\business;

class TryNotice extends \haibao\cms\business\Base{
	
	private static $_instance;
	
	/**
	 * 获取试用中心公告列表
	 */
	public static function getNoticeQuery($query){
		if(!$query){
			$query = new \haibao\cms\model\filter\TryNotice();
		}
		
		$noticeList = self::instance()->getNoticeByQuery($query);
	
		return $noticeList;
	}
	
	/**
	 * 根据id查询公告详细信息
	 */
	public static function getNoticeById($id){
		return self::instance()->getNoticeById($id);
	}
	
	/**
	 * 添加新的试用公告
	 * @param \haibao\cms\model\data\TryNotice $model
	 */
	public static function addTryNotice($model){
		self::checkOperation(self::FUNC_CODE_TRY_NOTICE_ADD);
		
		self::checkForm($model);
		$model->CreateTime = new \DateTime();
		$model->UserId = self::getCurrentUser()->Id;
		$model->Status = \haibao\cms\model\data\TryNotice::STATUS_ENABLE;
		
		self::instance()->addTryNotice($model);
		
		self::saveOperateHistory(self::FUNC_CODE_TAG_ADD, $model->toArray(true, true));
	}
	
	/**
	 * 修改使用公告信息
	 */
	public static function updateTryNotice($model){
		self::checkOperation(self::FUNC_CODE_TRY_NOTICE_EDIT);
		
		self::checkForm($model);
		$model->UserId = self::getCurrentUser()->Id;
		
		self::instance()->updateNoitceById($model);
		
		self::saveOperateHistory(self::FUNC_CODE_TAG_EDIT, $model->toArray(true, true));
	}
	
	/**
	 * 删除公告
	 */
	public static function deleteTryNotice($id){
		self::checkOperation(self::FUNC_CODE_TRY_NOTICE_DELETE);
		
		if(!$id){
			return false;
		}
		self::instance()->deleteNotice($id);
		
		self::saveOperateHistory(self::FUNC_CODE_TAG_DELETE, $id);
	}
	
	private static function instance(){
		if(!self::$_instance){
			self::$_instance = new \haibao\cms\data\TryNotice();
		}
		return self::$_instance;
	}
	
	/**
	 * 验证表单
	 */
	private static function checkForm($model){
		if(!$model->Title){
			throw new BusinessException('公告标题不能为空');
		}
		if(!$model->Url){
			throw new BusinessException('公告链接不能为空');
		}
	}
	
}